package com.authenticateCenter.assembler;

import com.authenticateCenter.domain.entity.Authority;
import com.authenticateCenter.domain.entity.AuthorityModule;
import com.authenticateCenter.domain.vo.AuthorityModuleVo;
import com.authenticateCenter.service.IAuthorityModuleService;
import com.authenticateCenter.service.IAuthorityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.mybaits.CustomPage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.authenticateCenter.factory.AuthorityFactory.AUTHORITY_CONVERT;
import static com.authenticateCenter.factory.AuthorityModuleFactory.AUTHORITY_MODULE_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/10/21 20:01
 */
@Component
@RequiredArgsConstructor
public class AuthorityModuleAssembler implements InfoAssembler<AuthorityModuleVo,Integer, QueryWrapper<AuthorityModule>>{
    private IAuthorityModuleService iAuthorityModuleService;
    private IAuthorityService iAuthorityService;

    /**
     * @param example 组装条件
     * @return 返回组装的结果
     */
    @Override
    public AuthorityModuleVo assembler(Integer example) {
        return null;
    }

    /**
     * 1.查询出所有顶层资源模块 <br>
     * 2.查询出每个顶层资源模块底下所有资源<br>
     * 3.查询出每个顶层资源模块底下所有子模块<br>
     * 4.将2,3步骤查询出来结果组装到当前顶层资源模块<br>
     * 5.对当前顶层模块下子模块重复2,3,4步骤
     * @param customPage 分页信息
     * @param example    组装条件
     * @return 返回组装的多个结果
     */
    @Override
    public List<AuthorityModuleVo> assemblers(CustomPage customPage, QueryWrapper<AuthorityModule> example) {
       return AuthorityModuleVoBuilder.builder().setService(iAuthorityModuleService,iAuthorityService)
                .buildSonResourcesFirst().buildSonModuleSecond().build();
    }


    private static class AuthorityModuleVoBuilder{
        private IAuthorityModuleService iAuthorityModuleService;
        private IAuthorityService iAuthorityService;
        /**
         * 根据条件查询出来的所有资源模块
         */
        private List<AuthorityModule> authorityModules;
        /**
         * 要返回的结果集合
         */
        private List<AuthorityModuleVo> authorityModuleVoList=new ArrayList<>();

        public static AuthorityModuleVoBuilder builder(){
            return new AuthorityModuleVoBuilder();
        }

        public AuthorityModuleVoBuilder setService(IAuthorityModuleService iAuthorityModuleService,IAuthorityService iAuthorityService){
            this.iAuthorityModuleService=iAuthorityModuleService;
            this.iAuthorityService=iAuthorityService;
            return this;
        }

        /**
         * 根据条件查询出资源模块
         */
        public AuthorityModuleVoBuilder buildInitAuthorityModuleVoList(QueryWrapper<AuthorityModule> queryWrapper){
            authorityModules = iAuthorityModuleService.list(queryWrapper);
            return this;
        }

        /**
         *  构建子资源
         */
        public AuthorityModuleVoBuilder buildSonResourcesFirst(){
            authorityModules.forEach(authorityModule -> {
                AuthorityModuleVo authorityModuleVo = buildResourceUnderAuthorityModule(authorityModule.getId());
                authorityModuleVoList.add(authorityModuleVo);
            });
            return this;
        }

        /**
         *  构建子模块
         */
        public AuthorityModuleVoBuilder buildSonModuleSecond(){
            authorityModules.forEach(authorityModule -> {
                //装配子模块
                authorityModuleVoList.forEach(authorityModuleVo -> {
                    List<AuthorityModuleVo> authorityModuleVos = buildSonModuleUnderAuthorityModule(authorityModule.getId());
                    authorityModuleVo.setAuthorityModuleVoList(authorityModuleVos);
                    //子模块递归装配
                    authorityModuleVos.forEach(this::buildSonModuleRecursion);
                });
            });
            return this;
        }

        public List<AuthorityModuleVo> build(){
            return authorityModuleVoList;
        }

        public void buildSonModuleRecursion(AuthorityModuleVo sonModule){
            sonModule.setAuthorityVoList(buildResourceUnderAuthorityModule(sonModule.getId()).getAuthorityVoList());
            List<AuthorityModuleVo> sonModules = buildSonModuleUnderAuthorityModule(sonModule.getId());
            sonModule.setAuthorityModuleVoList(sonModules);
            sonModules.forEach(this::buildSonModuleRecursion);
        }

        /**
         * 查询出当前资源模块底下所有资源,并赋值给当前资源模块
         * @return
         */
        public AuthorityModuleVo buildResourceUnderAuthorityModule(Integer moduleId){
            List<Authority> authorityList = iAuthorityService.list(new QueryWrapper<Authority>()
                    .eq("module_id", moduleId));
            return AuthorityModuleVo.builder().authorityVoList(AUTHORITY_CONVERT.toListAuthorityVo(authorityList)).build();
        }

        /**
         * 查询出每个顶层资源模块底下所有子模块,并赋值给当前资源模块<br>
         * @return
         */
        public List<AuthorityModuleVo> buildSonModuleUnderAuthorityModule(Integer moduleId){
            return AUTHORITY_MODULE_CONVERT.toAuthorityModuleVoList(iAuthorityModuleService.list(new QueryWrapper<AuthorityModule>()
                    .eq("parent_id", moduleId)));
        }
    }
}
